package com.mxx.common.conv;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.mxx.common.utils.date.DateFormatterUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        super();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer((DateFormatterUtil.dateTimeFormatter())));
        simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer((DateFormatterUtil.dateTimeFormatter())));
        simpleModule.addSerializer(LocalDate.class, new LocalDateSerializer((DateFormatterUtil.dateFormatter())));
        simpleModule.addDeserializer(LocalDate.class, new LocalDateDeserializer((DateFormatterUtil.dateFormatter())));
        simpleModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer((DateFormatterUtil.timeFormatter())));
        simpleModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateFormatterUtil.timeFormatter()));
        simpleModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateFormatterUtil.timeFormatter()));
//        this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        simpleModule.addDeserializer(Boolean.class, new BooleanDeserializer());
        registerModule(simpleModule);
    }
}
